package com.hz.foodstalls.model;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.hz.foodstalls.model.base.BaseWxUser;
import com.jfinal.ext.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.SnsApi;
import com.jfinal.weixin.sdk.api.UserApi;
import org.apache.commons.lang3.StringUtils;

/**
 * Model, Generated by Robot on 2017-5-10 10:51:09.
 */
@SuppressWarnings("serial")
public class WxUser extends BaseWxUser<WxUser> {
    public static final WxUser dao = new WxUser();

    public Page<WxUser> page(int pageNumber, int pageSize, String id) {
        return WxUser.dao.paginate(pageNumber, pageSize, "select *", "from wx_user where wechat_info_id = ?", id);
    }
    public Page<WxUser> page1(int pageNumber, int pageSize, String id,String prjName) {
        return WxUser.dao.paginate(pageNumber, pageSize, "select *", "from wx_user where wechat_info_id = ? and nickname like '%" + prjName + "%'", id);
    }

    public Page<WxUser> page(int pageNumber, int pageSize, String orderField, String orderDirection, String where) {
        return WxUser.dao.paginate(pageNumber, pageSize, "select * ",
                String.format("from wx_user where 1=1 %s order by %s %s", where, orderField, orderDirection));
    }

    public int batchDelete(String ids) {
        return Db.update(String.format("delete from wx_user where id in (%s) ", ids));
    }

    public boolean saveOrUpdate(WxUser model, boolean isSave) {
        boolean flag = false;
        if (model.getId() == null) {
            // model.setCreateTime(new Date());
            flag = model.save();
        } else {
            // model.setEditTime(new Date());
            flag = model.update();
        }
        return flag;
    }

    public void saveWxUser(String openid, String appId) {
        ApiResult apiResult = UserApi.getUserInfo(openid);
        JSONObject object = (JSONObject) JSONObject.parse(apiResult.getJson());
        if(StringUtils.isNotBlank(object.getString("errcode")))
            return;
        WechatInfo info = WechatInfo.dao.findFirst("select * from wechat_info where appid=?", appId);
        WxUser wxUser1 = dao.findFirst("select *from wx_user where openid=?", openid);
        if (wxUser1 == null) {
            WxUser wxUser = new WxUser();
            wxUser.setCity(object.getString("city"));
            wxUser.setWechatInfoId(info.getId());
            wxUser.setCountry(object.getString("country"));
            wxUser.setLanguage(object.getString("language"));
            wxUser.setNickname(StrKit.filterEmoji(object.getString("nickname")));
            wxUser.setHeadimg(object.getString("headimgurl"));
            wxUser.setOpenid(object.getString("openid"));
            wxUser.setProvince(object.getString("province"));
            wxUser.setRemark(object.getString("remark"));
            wxUser.setSex(object.getInteger("sex"));
            wxUser.setSubscribeTime(object.getInteger("subscribe_time"));
            wxUser.save();
        }

    }

    public void saveWxUser2(String openid, String appId, String access_token) {
        ApiResult apiResult = SnsApi.getUserInfo(access_token, openid);
        JSONObject object = (JSONObject) JSONObject.parse(apiResult.getJson());

        WechatInfo info = WechatInfo.dao.findFirst("select * from wechat_info where appid=?", appId);
        WxUser wxUser1 = dao.findFirst("select *from wx_user where openid=?", openid);
        if (wxUser1 == null) {
            WxUser wxUser = new WxUser();
            wxUser.setCity(object.getString("city"));
            wxUser.setWechatInfoId(info.getId());
            wxUser.setCountry(object.getString("country"));
            wxUser.setLanguage(object.getString("language"));
            wxUser.setNickname(object.getString("nickname"));
            wxUser.setHeadimg(object.getString("headimgurl"));
            wxUser.setOpenid(object.getString("openid"));
            wxUser.setProvince(object.getString("province"));
            wxUser.setRemark(object.getString("remark"));
            wxUser.setSex(object.getInteger("sex"));
            wxUser.setSubscribeTime(object.getInteger("subscribe_time"));
            wxUser.save();
        }

    }

    public boolean deleteByOpenId(String openid) {
        WxUser wxUser = dao.findFirst("select *from wx_user where openid=?", openid);
        return wxUser.delete();

    }

    public WxUser findByOpenId(String openid) {
        WxUser wxUser = dao.findFirst("select *from wx_user where openid=?", openid);
        return wxUser;
    }

    public String gettimes() {
        int time = getSubscribeTime();
        Long longtime = new Long((long) time) * 1000L;
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = format.format(new Date(longtime));
        return times;
    }

}
